use apilogger::models::RequestLog;
use axum::http::{HeaderMap, Method, Uri};
use chrono::Utc;
use std::str::FromStr;

#[tokio::test]
async fn test_request_log_creation() {
    // 创建测试数据
    let method = Method::GET;
    let uri = Uri::from_str("/api/test").unwrap();
    let mut headers = HeaderMap::new();
    headers.insert("content-type", "application/json".parse().unwrap());
    let body = r#"{"test": "data"}"#.to_string();
    let remote_addr = "127.0.0.1:12345".to_string();

    // 创建请求日志
    let log = RequestLog::new(
        method.clone(),
        uri.clone(),
        headers.clone(),
        body.clone(),
        remote_addr.clone(),
        None,
    );

    // 测试日志内容
    assert_eq!(log.method, method);
    assert_eq!(log.uri, uri);
    assert_eq!(log.headers, headers);
    assert_eq!(log.body, body);
    assert_eq!(log.remote_addr, remote_addr);
    assert_eq!(log.json_file_path, None);
    
    // 测试时间戳在合理范围内
    let now = Utc::now();
    let diff = now.signed_duration_since(log.timestamp);
    assert!(diff.num_seconds() >= 0);
    assert!(diff.num_seconds() <= 5); // 假设测试在5秒内完成

    // 测试 JSON 序列化
    let json = serde_json::to_string_pretty(&log).unwrap();
    assert!(json.contains(r#""method": "GET""#));
    assert!(json.contains(r#""uri": "/api/test""#));
    assert!(json.contains(r#""body": "{\"test\": \"data\"}""#));
    assert!(json.contains(r#""remote_addr": "127.0.0.1:12345""#));
    assert!(!json.contains("json_file_path")); // 确保 None 值被跳过
} 